package com.iotmonitor.iot.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.Lists;
import com.iotmonitor.common.security.utils.SecurityUtils;
import com.iotmonitor.iot.domain.TbBoard;
import com.iotmonitor.iot.domain.vo.BoardStatus;
import com.iotmonitor.iot.service.ITbBoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.iotmonitor.common.log.annotation.Log;
import com.iotmonitor.common.log.enums.BusinessType;
import com.iotmonitor.common.security.annotation.RequiresPermissions;
import com.iotmonitor.common.core.web.controller.BaseController;
import com.iotmonitor.common.core.web.domain.AjaxResult;
import com.iotmonitor.common.core.utils.poi.ExcelUtil;
import com.iotmonitor.common.core.web.page.TableDataInfo;

/**
 * 面板配置Controller
 * 
 * @author ruoyi
 * @date 2023-08-11
 */
@RestController
@RequestMapping("/board")
public class TbBoardController extends BaseController
{
    @Autowired
    private ITbBoardService tbBoardService;

    /**
     * 查询面板配置列表
     */
    @RequiresPermissions("system:board:list")
    @GetMapping("/list")
    public TableDataInfo list(TbBoard tbBoard)
    {
        startPage();
        List<TbBoard> list = tbBoardService.selectTbBoardList(tbBoard);
        return getDataTable(list);
    }

    /**
     * 导出面板配置列表
     */
    @RequiresPermissions("system:board:export")
    @Log(title = "面板配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbBoard tbBoard)
    {
        List<TbBoard> list = tbBoardService.selectTbBoardList(tbBoard);
        ExcelUtil<TbBoard> util = new ExcelUtil<TbBoard>(TbBoard.class);
        util.exportExcel(response, list, "面板配置数据");
    }

    /**
     * 获取面板配置详细信息
     */
    @RequiresPermissions("system:board:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbBoardService.selectTbBoardById(id));
    }

    /**
     * 新增面板配置
     */
    @RequiresPermissions("system:board:add")
    @Log(title = "面板配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated TbBoard tbBoard)
    {
        List<TbBoard> tbBoards = tbBoardService.selectTbBoardList(new TbBoard(tbBoard.getName()));
        if (!tbBoards.isEmpty()){
            return error("已存在该名称");
        }
        tbBoard.setUserId(SecurityUtils.getUserId());
        return toAjax(tbBoardService.insertTbBoard(tbBoard));
    }

    /**
     * 修改面板配置
     */
    @RequiresPermissions("system:board:edit")
    @Log(title = "面板配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated TbBoard tbBoard)
    {
        return toAjax(tbBoardService.updateTbBoard(tbBoard));
    }

    /**
     * 删除面板配置
     */
    @RequiresPermissions("system:board:remove")
    @Log(title = "面板配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbBoardService.deleteTbBoardByIds(ids));
    }



    @GetMapping
    public AjaxResult getAll(){
        TbBoard tbBoard = new TbBoard();
        tbBoard.setSystem(false);
        return AjaxResult.success(tbBoardService.selectTbBoardList(tbBoard));
    }

    /**
     * 获取系统看板
     * @return
     */
    @GetMapping("/systemBoard")
    public AjaxResult getSystemBoard(){
        TbBoard tbBoard = new TbBoard();
        tbBoard.setDisable(false);
        tbBoard.setSystem(true);
        return AjaxResult.success(tbBoardService.selectTbBoardList(tbBoard));
    }

    /**
     * 删除看板
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id){
        TbBoard boardEntity = tbBoardService.selectTbBoardById(id);
        if(boardEntity.getSystem() == true){
            BoardStatus boardStatus = new BoardStatus();
            boardStatus.setBoardId(id);
            boardStatus.setDisable(true);
            List<BoardStatus> boardStatuses = Lists.newArrayList();
            boardStatuses.add(boardStatus);
            setStatus(boardStatuses);
            // TODO 返回一些东西
            return null;
        }
        Long[] ids = {id};
        return remove(ids);
    }

    /**
     * 设置面板状态
     * @param
     * @return
     */
    @PutMapping("/status")
    public Boolean setStatus(@RequestBody List<BoardStatus> boardStatusList){
        boardStatusList.forEach(x->{
            TbBoard boardEntity = tbBoardService.selectTbBoardById(x.getBoardId());
            if (boardEntity != null){
                boardEntity.setDisable(x.getDisable());
                tbBoardService.updateTbBoard(boardEntity);
            }
        });
        return true;
    }

}
